package org.nanyuan.forum.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.nanyuan.forum.common.AppResult;
import org.nanyuan.forum.common.ResultCode;
import org.nanyuan.forum.dao.BoardMapper;
import org.nanyuan.forum.exception.ApplicationException;
import org.nanyuan.forum.model.Board;
import org.nanyuan.forum.service.IBoardService;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        //非空校验
        if (num <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao查询数据库
        List<Board> result = boardMapper.selectByNum(num);
        //返回查询结果
        return result;
    }

    @Override
    public Board selectById(Long id) {
        //非空校验
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //调用dao查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空校验
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString(),"boardId:{}",id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        //调用dao执行更新
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断结果
        if (row != 1) {
            //打印日志
            log.warn("{},更新失败", ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询板块详情
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        //确保结果的正确行
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        if (updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }
        //调用dao
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (row != 1) {
            //打印日志
            log.warn("{},删除失败", ResultCode.FAILED.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
